

clear all
cap log close

log using "${logfiles}step08.log", replace


set seed 123456 

*** Nonworkplace violence firm construction
forvalues Y=2006/2014{
use "W:\workplace\data\altcounter_sample.dta", clear 
keep if crime_type== 1
drop shnro 
keep if year_event==`Y'
keep if suspect_employed==1
drop if wp_crime==1 
rename re_shnro shnro 
sort shnro year_event 
*** merge with non workplace violence defendant firm information 
merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`Y'.dta", keepusing(sykstun)
drop if _merge==2 
drop _merge 

drop if sykstun==""
tempfile cal`Y'
save `cal`Y'', replace 

		
} 

clear 
forvalues Y=2006/2014{
append using `cal`Y''	
} 



gen mm=suspect_sex=="1" & plaintiff_sex=="1"
gen mf=suspect_sex=="1" & plaintiff_sex=="2"

* keep only violent firms
collapse (mean) wp_crime (max) mm (max) mf, by(sykstun year_event)

gen year=year_event  
save "$dataout\treatment_nonworkplace_firms.dta", replace 



* find firm matches 
forvalues Y=2006 (1) 2013 {

use "$dataout\treatment_nonworkplace_firms.dta", clear
keep sykstun year_event year wp_crime  mm mf 
replace wp_crime=1 

keep if year_event==`Y'


local minus1=`Y'-1 
local minus2=`Y'-2 
local minus3=`Y'-3 
local minus4=`Y'-4 
local minus5=`Y'-5 

        * merge with past 5 years firm level characteristics to do nn matching 
		merge m:1 sykstun using "${dataout}plant_char_`Y'", keepusing (employee female_employee switch newhire earnings nace switch_female female_newhire)
		drop _merge
		
		
		rename earnings earnings_0
		rename employee employee_0
		rename female_employee female_employee_0
		rename switch switch_0
		rename newhire newhire_0
		rename nace nace_0 
	
		gen share_female_0=female_employee_0/employee_0
		
		
		merge m:1 sykstun using "${dataout}plant_char_`minus1'" , keepusing (employee female_employee switch newhire earnings nace switch_female female_newhire)
		keep if _merge==3
		drop _merge
		
		rename earnings earnings_1
		rename employee employee_1
		rename female_employee female_employee_1
		rename switch switch_1
		rename newhire newhire_1
		rename female_newhire female_newhire_1
		rename nace nace_1
		rename switch_female switch_female_1
		
		gen share_female_1=female_employee_1/employee_1
		
		merge m:1 sykstun using "${dataout}plant_char_`minus2'" , keepusing (employee female_employee switch newhire earnings nace switch_female female_newhire)
		keep if _merge==3
		drop _merge
		
		rename earnings earnings_2
		rename employee employee_2
		rename female_employee female_employee_2
		rename switch switch_2
		rename newhire newhire_2
		rename female_newhire female_newhire_2
		rename nace nace_2
		rename switch_female switch_female_2

		gen share_female_2=female_employee_2/employee_2
		
		merge m:1 sykstun using "${dataout}plant_char_`minus3'" , keepusing (employee female_employee switch newhire earnings nace switch_female female_newhire)
		keep if _merge==3
		drop _merge
		
		rename earnings earnings_3
		rename employee employee_3
		rename female_employee female_employee_3
		rename female_newhire female_newhire_3
		rename switch switch_3
		rename newhire newhire_3
		rename nace nace_3
		rename switch_female switch_female_3
		gen share_female_3=female_employee_3/employee_3
		
		merge m:1 sykstun using "${dataout}plant_char_`minus4'" , keepusing (employee female_employee switch newhire earnings nace switch_female female_newhire)
		keep if _merge==3
		drop _merge
		
		rename earnings earnings_4
		rename employee employee_4
		rename female_employee female_employee_4
		rename female_newhire female_newhire_4
		rename switch switch_4
		rename newhire newhire_4
		rename nace nace_4
		rename switch_female switch_female_4
		gen share_female_4=female_employee_4/employee_4
		
		merge m:1 sykstun using "${dataout}plant_char_`minus5'" , keepusing (employee female_employee switch newhire earnings nace switch_female female_newhire)
		keep if _merge==3
		drop _merge
		
		rename earnings earnings_5
		rename employee employee_5
		rename female_employee female_employee_5
		rename female_newhire female_newhire_5
		rename switch switch_5
		rename newhire newhire_5
		rename nace nace_5
		rename switch_female switch_female_5
		gen share_female_5=female_employee_5/employee_5
		
		
		gen share_newhire_1=newhire_1/employee_0
		gen share_newhire_2=newhire_2/employee_0
		gen share_newhire_3=newhire_3/employee_0
		gen share_newhire_4=newhire_4/employee_0
		gen share_newhire_5=newhire_5/employee_0
		
		
		gen share_fnewhire_1=female_newhire_1/employee_0
		gen share_fnewhire_2=female_newhire_2/employee_0
		gen share_fnewhire_3=female_newhire_3/employee_0
		gen share_fnewhire_4=female_newhire_4/employee_0
		gen share_fnewhire_5=female_newhire_5/employee_0
		
	
		gen share_switch_1=switch_1/employee_0
		gen share_switch_2=switch_2/employee_0
		gen share_switch_3=switch_3/employee_0
		gen share_switch_4=switch_4/employee_0
		gen share_switch_5=switch_5/employee_0
		
		gen share_switch_female_1=switch_female_1/employee_0
		gen share_switch_female_2=switch_female_2/employee_0
		gen share_switch_female_3=switch_female_3/employee_0
		gen share_switch_female_4=switch_female_4/employee_0
		gen share_switch_female_5=switch_female_5/employee_0
		

		
		ereplace year=max(year)
		

	replace wp_crime=0 if wp_crime==.
	drop if _n>100000 & wp_crime==0

	

	gen log_ave_earnings_1=log(earnings_1+1)
	gen log_ave_earnings_2=log(earnings_2+1)
	gen log_ave_earnings_3=log(earnings_3+1)
	gen log_ave_earnings_4=log(earnings_4+1)
	gen log_ave_earnings_5=log(earnings_5+1)
	
	
	
	gen log_employee_1=log(employee_1)
	gen log_employee_2=log(employee_2)
	gen log_employee_3=log(employee_3)
	gen log_employee_4=log(employee_4)
	gen log_employee_5=log(employee_5)
	
	
	drop if sykstun==""
	gen outcomes=1
	
    sort sykstun wp_crime 
	
	capture noisily teffects nnmatch (outcomes log_employee_* log_ave_earnings* share_female_* share_newhire_* nace_* share_switch_*) (wp_crime),  osample (new)
	
	* drop observations that violate overlap assumptions
	drop if new==1

	teffects nnmatch (outcomes log_employee_* log_ave_earnings* share_female_* share_newhire_* nace_* share_switch_*) (wp_crime),  gen (match_id)
   
	* Find their matches and matched ids
	save "$dataout\complete", replace 
	gen rownum=_n
	
	preserve 
	keep if wp_crime==1 & !missing(match_id1)
	save "$dataout\treatment", replace
	restore 
	
	keep if wp_crime==0 & !missing(match_id1)
	drop match_id1
	rename rownum match_id1
	
	save "$dataout\control", replace
	
	merge 1:m match_id1 using "$dataout\treatment", keepusing (match_id1)
	keep if _merge==3
	drop _merge
	tab match_id1
	
	append using "$dataout\treatment"
	
	save "$dataout\firm_allmatches_long_`Y'_control.dta", replace
  
  
  
}


* Create unique match_ids
forvalues Y=2006 (1) 2013 {
    use "$dataout\firm_allmatches_long_`Y'_control.dta", clear
		*** make sure each match has a unique id 
	sort match_id1 wp_crime 
	bysort match_id1: gen temp=_N
    sum match_id1, d
	gen max_match_id1=r(max)
	bysort match_id1: gen match_id1_temp=match_id1+max_match_id1 if temp==4 & (_n==2 | _n==4)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+1 if temp==6 & (_n==2 | _n==5)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+2 if temp==6 & (_n==3 | _n==6)
	
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+3 if temp==8 & (_n==2 | _n==6)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+4 if temp==8 & (_n==3 | _n==7)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+5 if temp==8 & (_n==4 | _n==8)
	
	
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+6 if temp==10 & (_n==2 | _n==7)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+7 if temp==10 & (_n==3 | _n==8)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+8 if temp==10 & (_n==4 | _n==9)
	bysort match_id1: replace match_id1_temp=match_id1+max_match_id1+9 if temp==10 & (_n==5 | _n==10)
	
	replace  match_id1= match_id1_temp if  match_id1_temp!=. 
	
	drop temp match_id1_temp
	bysort match_id1: gen temp=_N
	tab temp 
	drop temp max_match_id1
	save "$dataout\firm_allmatches_`Y'_control.dta",replace 

} 



// After finding the matches, link to firm data 5 years before and 5 years after the event
forvalues Y=2006 (1) 2013 {

	forvalues a= -5(1)5{
 
	use "$dataout\firm_allmatches_`Y'_control.dta", clear
	drop employee* earnings* share_female*
	
	
	gen time=`a'
	ereplace year_event=max(year_event)
	
	local k=`Y'+`a'

   
	merge m:1 sykstun using "${dataout}plant_char_`k'"
	drop if _merge==2
	

	save "$dataout\firm_allmatches_long_`Y'_`k'_control.dta", replace
    
	
	}

}



// Combine all years to form the firm panel data 
use "$dataout\firm_allmatches_long_2013_2018_control.dta", clear

	forvalues i=-5 (1)4 {
	local a=2013+`i'
	append using "$dataout\firm_allmatches_long_2013_`a'_control.dta"
}



forvalues Y=2006 (1) 2012 {
	forvalues i=-5(1) 5{
		local a=`Y'+`i'
	append using "$dataout\firm_allmatches_long_`Y'_`a'_control.dta"
}
}


replace year=year_event+time




gen baseline_employee=employee if time==0 
sort sykstun year
bysort sykstun: replace baseline_employee=employee[_n-1] if employee==. & time==0
bysort sykstun year_event: ereplace baseline_employee=max(baseline_employee)



*Generate base year total employee 
gen baseline_fe=female_employee if time==0 
sort sykstun year
bysort sykstun: replace baseline_fe=female_employee[_n-1] if female_employee==. & time==0
bysort sykstun year_event: ereplace baseline_fe=max(baseline_fe)



*Generate base year total employees
gen baseline_me=male_employee if time==0 
sort sykstun year
bysort sykstun: replace baseline_me=male_employee[_n-1] if male_employee==. & time==0
bysort sykstun year_event: ereplace baseline_me=max(baseline_me)

gegen match_id1=group(match_id1 year_event), replace 


save "${dataout}nonworkplace_firms_matched.dta", replace 
